package library.router.interceptor;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;

/**
 * @author Created by qiang.hou on 2018/12/27.
 * @version 1.0
 */
public abstract class AbstractLoginInterceptor extends AbstractSimpleInterceptor {
    /**
     * default login path extra
     */
    public final static int DEFAULT_LOGIN_EXTRA = 2018;
    /**
     * default login path group
     */
    public final static String DEFAULT_LOGIN_GROUP = "loginGroup";
    /**
     * target path
     */
    private final static String TARGET_PATH = "target_path";

    @Override
    protected void handleIntercept(Postcard postcard) {
        String simplePath = loginPath();
        if (null == simplePath || simplePath.equals("")) {
            return;
        }
        ARouter.getInstance().build(simplePath)
                .withString(TARGET_PATH, postcard.getPath()).navigation();
    }

    @Override
    protected boolean needIntercept() {
        return isCurrentLogin();
    }

    @Override
    protected int interceptExtra() {
        return loginInterceptExtra() > 0 ? loginInterceptExtra() : DEFAULT_LOGIN_EXTRA;
    }

    @Override
    protected String interceptGroup() {
        return loginInterceptGroup() != null ? loginInterceptGroup() : DEFAULT_LOGIN_GROUP;
    }

    /**
     * @return login intercept extra value
     */
    protected abstract int loginInterceptExtra();

    /**
     * @return login intercept group value
     */
    protected abstract String loginInterceptGroup();

    /**
     * login path
     *
     * @return login path
     */
    protected abstract String loginPath();

    /**
     * if already login
     *
     * @return boolean
     */
    protected abstract boolean isCurrentLogin();
}
